import { IMyApp } from "../../../app"
import { IAddress} from "./IAddress";
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";

const app = getApp<IMyApp>()

Page({
  action:"",
  data: {
    addressModel: {} as IAddress,
    name_invaid:"",
    phone_invaid:"",
    address_invaid: ""
  },
  onLoad(option: any) {
    this.action = option.action
    if(this.action == "add"){
      this.setData({
        addressModel: {
          id: "",
          name: "",
          phone: "",
          address: ""
        }
      });
    }else if(this.action == "edit"){
      let id = option.id;
      this.loadForm(id);
    }
  },
  onFieldInput(e: any){
    let fieldId = e.currentTarget.id;
    let value = e.detail;
    let model = this.data.addressModel;
    switch(fieldId){
      case "name":
        model.name = value;
        break;
      case "phone":
        model.phone = value;
        break;
      case "address":
        model.address = value;
        break;
    }
  },
  // 编辑的时候，按主键id载入数据
  loadForm(addressId: string){
    const url = app.globalData.apisite + "/appletapi/address/get";
    app.http.postRequest(url, { id: addressId}).then(json => {
      if (!json.flag) {
       
      }else{
        const data = json.data;
        this.setData({
          addressModel: {
            id: data.ADD_ID,
            name: data.ADD_NAME,
            phone: data.ADD_PHONE,
            address: data.ADD_ADDRESS,
          }
        });
      }
    });
  },

  // 保存按钮按下去
  saveAddressBtnClicked(){
    this.setData({
      name_invaid:"",
      phone_invaid:"",
      address_invaid: "",
    });
    var isOK = true;
    if (this.data.addressModel.name?.length == 0){
      this.setData({
        name_invaid: "姓名不能为空"
      });
      isOK = false;
    }
    if (this.data.addressModel.phone ?.length != 11) {
      this.setData({
        phone_invaid: "手机号码长度可能错误，应该是11位数"
      });
      isOK = false;
    }
    if (this.data.addressModel.phone?.length == 0) {
      this.setData({
        phone_invaid: "手机号码不能为空"
      });
      isOK = false;
    }
    if (this.data.addressModel.address?.length == 0) {
      this.setData({
        address_invaid: "地址不能为空"
      });
      isOK = false;
    }
    if (isOK){
      this.saveForm();
    }
  },

  saveForm() {
    let self = this;
    let url = app.globalData.apisite + '/appletapi/address/save?action='+ self.action;
    let formdata = self.data.addressModel;

    app.http.postRequest(url, formdata).then(json => {
      if (json.flag) {
        Toast({
          type: 'success',
          message: '保存成功',
          onClose: () => {
            const eventChannel = self.getOpenerEventChannel();
            eventChannel.emit('acceptDataFromOpenedPage', { data: self.data.addressModel });
            wx.navigateBack({ delta: 1 });
          }
        });
      }else{
        Toast("保存失败:" + json.msg);
      }
    });
  },

  removeAddressBtnClicked(){
    let self = this;

    Dialog.confirm({
      title: '询问',
      message: '您确认要删除此收货地址吗？'
    }).then(() => {
      let url = app.globalData.apisite + '/appletapi/address/remove';
      let formdata = { id: self.data.addressModel.id };
      app.http.postRequest(url, formdata).then(json => {
        if (json.flag) {
          Toast({
            type: 'success',
            message: '已删除',
            onClose: () => {
              const eventChannel = self.getOpenerEventChannel();
              eventChannel.emit('acceptDataFromOpenedPage', { data: 'delete success' });
              wx.navigateBack({ delta: 1 });
            }
          });
        } else {
          Toast("保存失败:" + json.msg);
        }
      });

    }).catch(() => {
      // on cancel
    });
  }

  
})